package com.wecode.core.page;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * Page信息拦截封装
 *
 * @Author: zhenzhong.wang@honeywell.com
 * @Date: 2018/10/22 3:11 PM
 */
@Slf4j
public class PageInterceptor implements HandlerInterceptor {

    Logger logger = LoggerFactory.getLogger(PageInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 只拦截GET请求
        if (!"GET".equals(request.getMethod())) {
            return true;
        }

        HandlerMethod method;
        try {
            method = (HandlerMethod) handler;
        } catch (Exception e) {
            logger.error("Page拦截出现异常");
            return true;
        }

        if (method.getMethodAnnotation(Page.class) == null) {
            return true;
        }

        // 创建PageSearch对象存储Page信息
        PageSearch ps = new PageSearch();
        ps.setOpsType(request.getParameter("opsType") == null ?
                PageAction.LIST : PageAction.valueOf(request.getParameter("opsType")));

        // 分页操作
        if (PageAction.LIST.equals(ps.getOpsType())) {

            if (request.getParameter("pageSize") != null) {
                ps.setPageSize(Integer.valueOf(request.getParameter("pageSize")));
            }
            if (request.getParameter("pageNumber") != null) {
                int pi = Integer.valueOf(request.getParameter("pageNumber")) - 1;
                ps.setPageNumber(pi < 0 ? 0 : pi);
            }
        }

        HashMap<String, Object> pageParameter = new HashMap<>();
        HashMap<String, Object> exportParameter = new HashMap<>();
        Enumeration<String> parameterNames = request.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String parameterName = parameterNames.nextElement();
            // 封装分页参数
            if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("page_")) {
                pageParameter.put(parameterName.substring(5), request.getParameter(parameterName));
            }

            // 封装导出参数
            if (StringUtils.isNotEmpty(parameterName) && parameterName.startsWith("expo_")) {
                exportParameter.put(parameterName.substring(5), request.getParameter(parameterName));
            }
        }
        ps.setPageParameter(pageParameter);
        ps.setExportParameter(exportParameter);

        PageHolder.setHolder(ps);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
